package com.ashokvarma.bottomnavigation.entry.slice;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.ShapeBadgeItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.ashokvarma.bottomnavigation.entry.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ListDialog;

public class MainAbilitySlice extends AbilitySlice {
    String[] modeItems = new String[] {"DEFAULT", "FIXED", "SHIFTING", "FIXED_NO_TITLE", "SHIFTING_NO_TITLE"};

    String[] backgroundStyleItems = new String[] {"DEFAULT", "STATIC", "RIPPLE"};

    String[] countItems = new String[] {"3 items", "4 items", "5 items"};

    String[] shapeBadgeItems =
            new String[] {
                "OVAL", "RECTANGLE", "HEART", "STAR_3_VERTICES", "STAR_4_VERTICES", "STAR_5_VERTICES", "STAR_6_VERTICES"
            };

    BottomNavigationBar bottomNavigationBar;
    Text modeText;
    Text backgroundSytleText;
    Text countText;
    Text shapeBadgeText;
    Text toggleBadge;
    Text toggleHide;
    Text touchEffect;
    Text select;

    int mode = BottomNavigationBar.MODE_DEFAULT;
    int backgroundSytle = BottomNavigationBar.BACKGROUND_STYLE_DEFAULT;
    int countItem = 0;
    int shapeBadge = ShapeBadgeItem.SHAPE_OVAL;

    TextBadgeItem numberBadgeItem;
    ShapeBadgeItem shapeBadgeItem;

    ShapeElement background;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        modeText = (Text) findComponentById(ResourceTable.Id_mode);
        backgroundSytleText = (Text) findComponentById(ResourceTable.Id_backgroundstyle);
        countText = (Text) findComponentById(ResourceTable.Id_itemcount);
        shapeBadgeText = (Text) findComponentById(ResourceTable.Id_shapeBadge);
        toggleHide = (Text) findComponentById(ResourceTable.Id_toggle_hide);
        toggleBadge = (Text) findComponentById(ResourceTable.Id_toggle_badge);
        touchEffect = (Text) findComponentById(ResourceTable.Id_toggle_toucheffect);
        select = (Text) findComponentById(ResourceTable.Id_select);
        bottomNavigationBar = (BottomNavigationBar) findComponentById(ResourceTable.Id_bar);

        modeText.setText(modeItems[0]);
        backgroundSytleText.setText(backgroundStyleItems[0]);
        countText.setText(countItems[0]);
        shapeBadgeText.setText(shapeBadgeItems[0]);

        background = new ShapeElement();
        background.setCornerRadius(dp2px(5));
        background.setRgbColor(RgbColor.fromArgbInt(0xff5EE1FF));
        modeText.setBackground(background);
        backgroundSytleText.setBackground(background);
        countText.setBackground(background);
        shapeBadgeText.setBackground(background);

        modeText.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        ListDialog listDialog = new ListDialog(MainAbilitySlice.this);
                        listDialog.setItems(modeItems);
                        listDialog.setOnSingleSelectListener(
                                new IDialog.ClickedListener() {
                                    @Override
                                    public void onClick(IDialog iDialog, int i) {
                                        mode = i;
                                        modeText.setText(modeItems[i]);
                                        refresh();
                                        listDialog.hide();
                                    }
                                });
                        listDialog.show();
                    }
                });
        backgroundSytleText.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        ListDialog listDialog = new ListDialog(MainAbilitySlice.this);
                        listDialog.setItems(backgroundStyleItems);
                        listDialog.setOnSingleSelectListener(
                                new IDialog.ClickedListener() {
                                    @Override
                                    public void onClick(IDialog iDialog, int i) {
                                        backgroundSytle = i;
                                        backgroundSytleText.setText(backgroundStyleItems[i]);
                                        refresh();
                                        listDialog.hide();
                                    }
                                });
                        listDialog.show();
                    }
                });

        countText.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        ListDialog listDialog = new ListDialog(MainAbilitySlice.this);
                        listDialog.setItems(countItems);
                        listDialog.setOnSingleSelectListener(
                                new IDialog.ClickedListener() {
                                    @Override
                                    public void onClick(IDialog iDialog, int i) {
                                        countItem = i;
                                        countText.setText(countItems[i]);
                                        refresh();
                                        listDialog.hide();
                                    }
                                });
                        listDialog.show();
                    }
                });

        shapeBadgeText.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        ListDialog listDialog = new ListDialog(MainAbilitySlice.this);
                        listDialog.setItems(shapeBadgeItems);
                        listDialog.setOnSingleSelectListener(
                                new IDialog.ClickedListener() {
                                    @Override
                                    public void onClick(IDialog iDialog, int i) {
                                        shapeBadge = i;
                                        shapeBadgeText.setText(shapeBadgeItems[i]);
                                        refresh();
                                        listDialog.hide();
                                    }
                                });
                        listDialog.show();
                    }
                });

        toggleHide.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        if (bottomNavigationBar != null) {
                            bottomNavigationBar.toggle();
                        }
                    }
                });

        toggleBadge.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        if (numberBadgeItem != null) {
                            numberBadgeItem.toggle();
                        }
                        if (shapeBadgeItem != null) {
                            shapeBadgeItem.toggle();
                        }
                    }
                });

        touchEffect.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        if (bottomNavigationBar != null) {
                            if (bottomNavigationBar.getTouchEffect()) {
                                bottomNavigationBar.setTouchEffect(false);
                            } else {
                                bottomNavigationBar.setTouchEffect(true);
                            }
                        }
                    }
                });

        bottomNavigationBar.setTabSelectedListener(
                new BottomNavigationBar.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(int position) {
                        select.setText("Current select: " + position);
                    }

                    @Override
                    public void onTabUnselected(int position) {}

                    @Override
                    public void onTabReselected(int position) {}
                });

        refresh();
    }

    public void refresh() {
        numberBadgeItem =
                new TextBadgeItem()
                        .setBorderWidth(dp2px(1.5f))
                        .setBackgroundColor(0xffff5454)
                        .setText("2")
                        .setHideOnSelect(true);

        shapeBadgeItem =
                new ShapeBadgeItem()
                        .setShape(shapeBadge)
                        .setShapeColor(0xffff5454)
                        .setOffset(dp2px(20), dp2px(-20))
                        .setHideOnSelect(true);

        bottomNavigationBar.clearAll();
        bottomNavigationBar.setBarMode(mode);
        bottomNavigationBar.setBackgroundStyle(backgroundSytle);
        if (countItem == 0) {
            bottomNavigationBar
                    .addItem(
                            new BottomNavigationItem(
                                            ResourceTable.Media_ic_location_on_white_24dp, "Nearby", getContext())
                                    .setActiveColor(0xffF57C00)
                                    .setBadgeItem(numberBadgeItem))
                    .addItem(
                            new BottomNavigationItem(
                                            ResourceTable.Media_ic_find_replace_white_24dp, "Find", getContext())
                                    .setActiveColor(0xff00796B))
                    .addItem(
                            new BottomNavigationItem(
                                            ResourceTable.Media_ic_favorite_white_24dp, "Categories", getContext())
                                    .setActiveColor(0xff2196F3)
                                    .setBadgeItem(shapeBadgeItem))
                    .setFirstSelectedPosition(0)
                    .initialise();
        } else if (countItem == 1) {
            bottomNavigationBar
                    .addItem(
                            new BottomNavigationItem(ResourceTable.Media_ic_home_white_24dp, "Home", getContext())
                                    .setActiveColor(0xffF57C00)
                                    .setBadgeItem(numberBadgeItem))
                    .addItem(
                            new BottomNavigationItem(ResourceTable.Media_ic_book_white_24dp, "Books", getContext())
                                    .setActiveColor(0xff00796B))
                    .addItem(
                            new BottomNavigationItem(
                                            ResourceTable.Media_ic_music_note_white_24dp, "Music", getContext())
                                    .setActiveColor(0xff2196F3)
                                    .setBadgeItem(shapeBadgeItem))
                    .addItem(
                            new BottomNavigationItem(ResourceTable.Media_ic_tv_white_24dp, "Movies & TV", getContext())
                                    .setActiveColor(0xff8D6E63))
                    .setFirstSelectedPosition(0)
                    .initialise();
        } else if (countItem == 2) {
            bottomNavigationBar
                    .addItem(
                            new BottomNavigationItem(ResourceTable.Media_ic_home_white_24dp, "Home", getContext())
                                    .setActiveColor(0xffF57C00)
                                    .setBadgeItem(numberBadgeItem))
                    .addItem(
                            new BottomNavigationItem(ResourceTable.Media_ic_book_white_24dp, "Books", getContext())
                                    .setActiveColor(0xff00796B))
                    .addItem(
                            new BottomNavigationItem(
                                            ResourceTable.Media_ic_music_note_white_24dp, "Music", getContext())
                                    .setActiveColor(0xff2196F3)
                                    .setBadgeItem(shapeBadgeItem))
                    .addItem(
                            new BottomNavigationItem(ResourceTable.Media_ic_tv_white_24dp, "Movies & TV", getContext())
                                    .setActiveColor(0xff8D6E63))
                    .addItem(
                            new BottomNavigationItem(
                                            ResourceTable.Media_ic_videogame_asset_white_24dp, "Games", getContext())
                                    .setActiveColor(0xff607D8B))
                    .setFirstSelectedPosition(0)
                    .initialise();
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    public int dp2px(float dp) {
        return (int) (getResourceManager().getDeviceCapability().screenDensity / 160 * dp);
    }
}
